﻿using FTBS.Models.Entities;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Data.Entity.Infrastructure;

namespace FTBS.Models.DAO
{
    public class TheaterDAO
    {
        private FilmTicketBookingSystemEntities _entities = new FilmTicketBookingSystemEntities();

        /// <summary>
        /// Get All Information Of Theater
        /// </summary>
        /// <returns>List of Theater</returns>
        /// <author>VuongVT</author>
        public List<Theater> GetAllTheaterInfo()
        {
            List<Theater> listTheater = new List<Theater>();
            var resultQuery = from theater in _entities.Theaters
                              where theater.IsDeleted == false
                              select theater;
            foreach (var theater in resultQuery)
            {
                listTheater.Add(theater);
            }
            return listTheater;
        }

        /// <summary>
        /// Get Theater by Id
        /// </summary>
        /// <param name="theaterId"></param>
        /// <returns></returns>
        public Theater GetTheaterById(int theaterId)
        {
            var resultQuery = (from theater in _entities.Theaters
                               where theater.TheaterId == theaterId
                                     && theater.IsDeleted == false
                               select theater).FirstOrDefault();
            return resultQuery;
        }

        /// <summary>
        /// Check exist TheaterName
        /// </summary>
        /// <param name="theater"></param>
        /// <returns>true: Exist TheaterName -- false: Not Exist TheaterName</returns>
        public bool IsExistTheater(Theater theater)
        {
            List<Theater> resultQuery = (from th in _entities.Theaters
                                        where th.TheaterName == theater.TheaterName
                                           && th.IsDeleted == false
                                        select th).ToList();
            if (resultQuery.Count() >= 1)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// Create a new Theater
        /// </summary>
        /// <param name="theater"></param>
        /// <returns>1: success -- 0: fail</returns>
        public int CreateTheater(Theater theater)
        {
            try
            {
                _entities.Theaters.Add(theater);
                return _entities.SaveChanges(); ;
            }
            catch (Exception)
            {
                return 0;
            }
        }

        /// <summary>
        /// Edit a Theater
        /// </summary>
        /// <param name="theater"></param>
        /// <returns>1: Success --- 0: Fail</returns>
        public int EditTheater(Theater theater)
        {
            try
            {
                var startingIssue = _entities.Theaters.Find(theater.TheaterId);
                _entities.Detach(startingIssue);
                _entities.Entry(theater).State = EntityState.Modified;
                return _entities.SaveChanges();
            }
            catch (Exception)
            {
                return 0;
            }
        }

        /// <summary>
        /// Delete a Theater
        /// </summary>
        /// <param name="theater"></param>
        /// <returns>1: Success --- 0: Fail</returns>
        public int DeleteTheater(Theater theater)
        {
            try
            {
                theater.IsDeleted = true;
                _entities.Entry(theater).State = EntityState.Modified;
                return _entities.SaveChanges();
            }
            catch (Exception)
            {
                return 0;
            }
        }

    }
}